package com.xmg.boot.demo.bootlesson1.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmg.boot.demo.bootcommon.utils.page.PageResult;
import com.xmg.boot.demo.bootlesson1.user.annotation.MyLog;
import com.xmg.boot.demo.bootlesson1.user.config.Friends;
import com.xmg.boot.demo.bootlesson1.user.annotation.StartSwaggerScan;
import com.xmg.boot.demo.bootlesson1.user.entity.Result;
import com.xmg.boot.demo.bootlesson1.user.entity.ResultUtil;
import com.xmg.boot.demo.bootlesson1.user.entity.pojo.User;
import com.xmg.boot.demo.bootlesson1.user.mapper.UserMapper;
import com.xmg.boot.demo.bootlesson1.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

/**
 * @author ma
 * @date 2023/11/15
 * @description 测试
 */

@StartSwaggerScan
@RestController
@EnableSwagger2 // 开启注解说明
public class UserController {
    @Value("${name:default}")
    private String name;

    @Value("${age:default}")
    private int age;

    @Value("${env.name}")
    private String info;

    @Autowired
    private Friends friends;

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;

    private Logger logger= LoggerFactory.getLogger(UserController.class);

    @ApiOperation(value="读配置文件-单个参数",notes="单参数切换配置环境")
    @GetMapping("/index")
    public String Index(){
        System.out.println(name+"-"+age+"-----"+info);
        return "OK";
    }

    @ApiOperation(value="读配置文件-对象参数",notes="对象参数")
    @GetMapping("/friend")
    public String Friend(){
        System.out.println(friends.toString());
        return friends.toString();
    }

    @ApiOperation(value = "logback日志写入",notes="地址为?info=xxx")
    @PostMapping("/logparam")
    public String LogParam(@RequestParam("info") String info){
        logger.info("RequestParam结果为：" + info);
        return "RequestParam结果为：" + info;
    }

    @ApiOperation(value = "logback日志写入",notes="地址为/infoXXXX")
    @GetMapping("/logpath/{info}")
    public String LogPathVariable(@PathVariable("info") String info){
        logger.trace("PathVariable结果为：" + info);
        return "PathVariable结果为：" + info;
    }

    @ApiOperation(value = "AOP切面")
    @GetMapping("/aspect")
    @MyLog(type = "aspect测试",explain = "log模拟日志生成")
    public String Aspect(){
        System.out.println("模拟测试开始");
        return "OK";
    }

    @ApiOperation(value = "AOP切面")
    @PutMapping("/aspectByParam")
    @MyLog(type = "AspectByParam方法",explain = "log模拟日志生成")
    public Result<User> AspectByParam(@RequestBody User user){
        User user1=new User();
        user1.setId(user.getId()+10);
        user1.setUserName(user.getUserName()+" -- 新增名称");
        user1.setUserGender(user.getUserGender()+" -- 新增性别");
        user1.setUserAge(user.getUserAge()+100);
        return ResultUtil.success(user1);
    }

    @ApiOperation("手动抛出异常")
    @GetMapping("/throw")
    public Result<String> ThrowException(){
        int a= 100/0;
        return ResultUtil.success(a);
    }

    @ApiOperation("插入用户信息")
    @PostMapping("/add")
    @MyLog(type = "添加用户",explain = "自定义用户对象插入")
    public Result<Boolean> AddUser(@RequestBody User user){
        boolean flag= userService.save(user);
        return ResultUtil.success(flag);
    }


    @ApiOperation("查询 - 根据用户名称")
    @GetMapping("/getUserListByUserName/{uName}")
    public Result<List<User>> getUserListByUserName(@PathVariable("uName") String uName){
        List<User> lst=userMapper.getUserByUserName(uName);
        return ResultUtil.success(lst);
    }

    @ApiOperation("查询 - 分页查询")
    @GetMapping("/page")
    public Result<Page<User>> getUserByPage(@RequestParam("userName") String userName
            ,@RequestParam("index") int index
            ,@RequestParam("size") int size){

        //第一种方法，手动写
        Page<User> pageLst = userService.getUserByPage(userName,index,size);
        PageResult<User> lst=PageResult.getPage(pageLst);

        //第二种方法，调用common库的分页结果集
        //PageResult<User> lst = userService.getCommonMethod(userName,index,size);
        return ResultUtil.success(lst);
    }
}
